/**
 * 
 */
package cz.geoserver.KMLpublisher.util;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Class holds incoming SQL queries for future loading of data in ConcurrentHashMap<String, SQLparser>
 * This class is a singleton for whole application
 * @author mkepka
 *
 */
public class QueryMapHolder {

    private static QueryMapHolder QUERYMAPHOLDER;
    private static final ConcurrentHashMap<String, SQLparser> sqlMap = new ConcurrentHashMap<String, SQLparser>();

    /**
     * Constructor creates instance of class 
     */
    private QueryMapHolder(){ 
    }

    /**
     * Method returns the only sole created instance of class.
     * If an instance have not been created yet it creates sole instance.
     * @return
     */
    public static synchronized QueryMapHolder getInstance(){
        if(QUERYMAPHOLDER==null){
            QUERYMAPHOLDER = new QueryMapHolder();
        }
        return QUERYMAPHOLDER;
    }

    /**
     * Method returns instance of SQLparser that is stored under given key. 
     * @param key - Key that identifies instance of SQLparser in Map
     * @return - SQlparser object with SQL query
     */
    public static synchronized SQLparser getSQLparser(String key){
        return sqlMap.get(key);
    }

    /**
     * Method stores given SQLparser object under given key in Map
     * @param key - Key that identifies instance of SQLparser in Map
     * @param query - SQlparser object with SQL query
     */
    public static synchronized void putSQLparser(String key, SQLparser query){
        sqlMap.put(key, query);
    }

    /**
     * Method stores given SQLparser object under created key in Map
     * Key is created as current Date in milis
     * @param query - SQlparser object with SQL query
     * @return key - String key is created as current Date in milis
     */
    public static synchronized String putSQLparser(SQLparser query){
        Date timestamp = new Date();
        String key = String.valueOf(timestamp.getTime());
        sqlMap.put(key, query);
        return key;
    }
}